<?php

require_once("util/scraper.php"); 

class priceChecker {
    
    private $m_shopList;
    private $m_priceList;
    private $m_deliveryPriceList;
    
    function __construct() {
        $this->m_shopList = array();
		$this->m_priceList = array();
		$this->m_deliveryPriceList = array();
    }
    
    public function dealString($start, &$res1, &$res2) {
        $even = true;
        $res1 = array();
        $res2 = array();
        foreach ($start as $val)
        {
            if($even)
                array_push($res1, $val);
            else
                array_push($res2, $val);
            $even = !$even;
        }
    }
    
    public function findPriceList($album, $artist) {
		$type = 'Scraper';
		$obj = new $type;
        
        $useAlbum = str_replace(' ', '+', $album, $i);
        $useArtist = str_replace(' ', '+', $artist, $i);
        
        $url = "http://www.kieskeurig.be/product/search/queryprices?qcode=cd&q=" . $useArtist . "+" . $useAlbum . "&pcode=cd&gp=" . $useArtist . "+" . $useAlbum;
        echo $url;	

        $start = '<a href="';
        $end = '"';
        $offset = '<td class="price">';
        $max = 1;
        
        $obj->initialize($url,$start, $end, $offset, $max );
        
        if($obj->searchForData()){
            $results = $obj->getResultList();
    		$url = "http://www.kieskeurig.be" . $results[0];
		}
		else{
			echo "<br/>could not find the product";
		}
        
        $start = 'name="shopDescription[]" value="';
        $end = '"';
        $offset = '<table class="sortableTable" id="priceTable">';
        $max = 10;
        $obj->initialize($url,$start, $end, $offset, $max );
        
        if($obj->searchForData()){
            $this->m_shopList = $obj->getResultList();
			print_r($this->m_shopList);
            echo "<br>";
		}
		else{
			echo "<br/>could not find the shops";
		}
        
        $start = 'target="_blank">&euro; ';
        $end = '<';
        $offset = '<table class="sortableTable" id="priceTable">';
        $max = 20;
        
        $obj->initialize($url,$start, $end, $offset, $max );
        
        if($obj->searchForData()){
            $pricelist = $obj->getResultList();
            $this->dealString($pricelist, $this->m_priceList, $this->m_deliveryPriceList);
			print_r($this->m_priceList);
            echo "<br>";
			print_r($this->m_deliveryPriceList);
            echo "<br>";
		}
		else{
			echo "<br/>could not find the shops";
		}
    }
}
?>